package com.mashibing;

import org.openjdk.jol.info.ClassLayout;

public class SynchronizedIntegerTest {
    static Integer integer=0;

  public static void main(String[] args) throws InterruptedException {
    //
    while (integer == 0) {
      Thread t1 =
          new Thread(
              () -> {
                for (int i = 0; i < 500; i++) {
                  synchronized (integer) {
                    integer++;
                    String toPrintable = ClassLayout.parseInstance(integer).toPrintable();
                    System.out.println("t1-->" + toPrintable);
                  }
                }
              },
              "t1");

      Thread t2 =
          new Thread(
              () -> {
                for (int i = 0; i < 500; i++) {
                  synchronized (integer) {
                    integer--;
                    System.out.println("t2-->" + integer.hashCode());
                  }
                }
              },
              "t2");

      t1.start();
      t2.start();
      t1.join();
      t2.join();
      System.out.println("最后结果" + integer);
    }
  }
}
